package com.nbsaas.boot.system;

import com.nbsaas.boot.rest.annotations.*;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.system.api.domain.request.MenuRequest;
import com.nbsaas.boot.system.api.domain.request.MenuSearch;
import com.nbsaas.boot.system.api.domain.response.MenuResponse;
import com.nbsaas.boot.system.api.domain.simple.MenuSimple;
import com.nbsaas.boot.system.api.apis.MenuApi;
import com.nbsaas.boot.system.ext.apis.MenuExtApi;
import com.nbsaas.boot.system.ext.domain.request.MenuDragRequest;
import com.nbsaas.boot.system.ext.domain.request.UpdateRoleMenuRequest;
import com.nbsaas.boot.system.ext.domain.simple.MenuExtSimple;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import jakarta.annotation.Resource;

import java.util.List;

/**
*  菜单控制器
*/
@RequiresAuthentication
@RestController
@RequestMapping("/menu")
public class MenuController {




    @Resource
    private MenuApi menuApi;

    @SearchData
    @PostMapping("/drag")
    public ResponseObject<?> drag(@RequestBody MenuDragRequest request) {
        return menuExtApi.drag(request);
    }

    @SearchData
    @PostMapping("/updateRoleMenus")
    public ResponseObject<?> updateRoleMenus(@RequestBody UpdateRoleMenuRequest request) {
        return menuExtApi.updateRoleMenus(request);
    }

    @SearchData
    @PostMapping("/selectPermissionByUser")
    public ListResponse<String> selectPermissionByUser(Long userId) {
        return menuExtApi.selectPermissionByUser(userId);
    }

    @SearchData
    @PostMapping("/permissions")
    public List<String> permissions(Long role) {
        return menuExtApi.permissions(role);
    }

    @SearchData
    @PostMapping("/selectForPermission")
    public ListResponse<Long> selectForPermission( Long role) {
        return menuExtApi.selectForPermission(role);
    }

    @SearchData
    @PostMapping("/findByRole")
    public ListResponse<MenuExtSimple> findByRole(Long role) {
        return menuExtApi.findByRole(role);
    }

    @Resource
    private MenuExtApi menuExtApi;

    @SearchData
    @RequiresPermissions("menu:search")
    @PostMapping("/root")
    public ListResponse<MenuSimple> root(@RequestBody MenuSearch search) {
        return menuApi.root(search);
    }

    /**
    * 搜索数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("menu:search")
    @PostMapping("/search")
    public PageResponse <MenuSimple> search(@RequestBody MenuSearch request) {
        return menuApi.search(request);
    }

    /**
    * 列表数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("menu:list")
    @PostMapping("/list")
    public ListResponse<MenuSimple> list(@RequestBody MenuSearch request) {
        return menuApi.list(request);
    }

    /**
    * 添加数据
    *
    * @param request
    * @return
    */
    @RequiresPermissions("menu:create")
    @CreateData
    @PostMapping("/create")
    public ResponseObject <MenuResponse> create(@RequestBody @Validated(AddOperator.class) MenuRequest request) {
        return menuApi.create(request);
    }

   /**
   * 修改数据
   * @param request
   * @return
   */
   @RequiresPermissions("menu:update")
   @UpdateData
   @PostMapping("/update")
   public ResponseObject<MenuResponse> update(@RequestBody @Validated(UpdateOperator.class) MenuRequest request) {
       return menuApi.update(request);
   }

    /**
    * 删除数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("menu:delete")
    @PostMapping("/delete")
    public ResponseObject<?> delete(@RequestBody @Validated(DeleteOperator.class) MenuRequest request) {
        return menuApi.delete(request);
    }

    /**
    * 查看数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("menu:view")
    @PostMapping("/view")
    public ResponseObject <MenuResponse> view(@RequestBody @Validated(ViewOperator.class) MenuRequest  request) {
        return menuApi.view(request);
    }
}